import type { REPORT_AUDIT_STATUS, REPORT_FORM_ID, REPORT_ID, REPORT_STATUS } from '@/enum/modules/report'
import { getAjaxBaseUrl } from '../handler'
import type { AjaxPagingResult } from '@/types'

const controller = getAjaxBaseUrl('/dataReport', '/report')

/** 数据填报-列表查询 */
export function dataReportQueryList(
  time: string,
  socialCreditCode: string,
  subsidiaryType: any,
  // 0-煤矿，1-洗煤厂
  type = 0 | 1,
  status?: REPORT_STATUS,
) {
  return get(`${controller}/list`, {
    time,
    socialCreditCode,
    type,
    status,
    subsidiaryType,
  })
}

/** 加载报表数据 */
export function loadReportDataByReportId(
  id: number,
  formId?: REPORT_FORM_ID,
) {
  return get(`${controller}/detail`, {
    id,
    formId,
  })
}

/** 数据填报-保存电量电费报表数据 */
export function saveReportDataOfCompanyElectricity(data: any) {
  return post(`${controller}/electricity/save`, data)
}
export function updateReportDataOfCompanyElectricity(data: any) {
  return post(`${controller}/electricity/update`, data)
}

/** 数据填报-保存水平衡报表数据 */
export function saveReportDataOfCompanyWeight(data: any) {
  return post(`${controller}/water/save`, data)
}
export function updateReportDataOfCompanyWeight(data: any) {
  return post(`${controller}/water/update`, data)
}

/** 数据填报-保存分类用电情况报表数据 */
export function saveReportDataOfCompanyElectricityCategory(data: any) {
  return post(`${controller}/electricitySort/save`, data)
}
export function updateReportDataOfCompanyElectricityCategory(data: any) {
  return post(`${controller}/electricitySort/update`, data)
}

/** 数据填报-保存主要耗能设备报表数据 */
export function saveReportDataOfCompanyPrimaryElectricityDevice(data: any) {
  return post(`${controller}/equipment/save`, data)
}
export function updateReportDataOfCompanyPrimaryElectricityDevice(data: any) {
  return post(`${controller}/equipment/update`, data)
}

/** 数据填报-上报报表数据 */
export function reportDataById(id: number, userName: string) {
  return get(`${controller}/bring`, { id, userName })
}

/** 数据填报-查询水平衡的历史值 */
export function loadHistoryDataOfWaterBalance(
  time: string,
  socialCreditCode: string,
  reportId: REPORT_ID,
) {
  return get(`${controller}/water/detail`, {
    time,
    socialCreditCode,
    reportId,
  })
}

/** 数据填报-查询分类用电的历史值 */
export function loadHistoryDataOfCompanyElectricityCategory(
  time: string,
  socialCreditCode: string,
  reportId: REPORT_ID,
) {
  return get(`${controller}/electricitySort/detail`, {
    time,
    socialCreditCode,
    reportId,
  })
}

/** 分页查询报表操作记录 */
export function queryReportRecordsByPage(id: number, pageNumber: number, pageSize: number) {
  return post<AjaxPagingResult<any>>(`${controller}/record/page`, {
    id,
    pageNumber,
    pageSize,
  })
}

/** 报表导出 */
export async function exportFileOfReport(id: number) {
  return await request({
    url: `${controller}/export`,
    method: 'GET',
    responseType: 'blob',
    params: { id },
  })
}

/** 报表审核-查询列表(树结构) */
export function queryReportAuditTree(data: {
  time: string
  status?: REPORT_AUDIT_STATUS
  socialCreditCode?: string
  reportName?: string
}) {
  return post<any>(`${controller}/auditReport`, data)
}

/** 报表审核-查询列表 */
export function queryReportAuditList(data: {
  time: string
  pageNumber: number
  pageSize: number
  status?: REPORT_AUDIT_STATUS
  socialCreditCode?: string
  reportName?: string
}) {
  return post<AjaxPagingResult<any>>(`${controller}/auditPage`, data)
}

/** 报表审核-审核 */
export function auditReport(data: {
  id: number
  // 0-通过，1-返回
  type: 0 | 1
  remark?: string
}) {
  return post(`${controller}/audit`, data)
}

/** 报表汇总-查询列表 */
export function queryReportSummaryList(time: string) {
  return get(`${controller}/totalReport/list`, { time })
}

/** 电量，电费汇总报表详情 */
export function loadReportDataOfSummaryCompanyElectricity(
  time: string,
  formId: REPORT_FORM_ID,
) {
  return get(`${controller}/totalDetail/electricity`, { time, formId })
}

/** 成本表数据保存 */
export function saveReportDataOfSummaryCompanyElectricityCost(data: {
  time: string
  electricityTariff: number
  dataList: any[]
}) {
  return post(`${controller}/electricityCost/save`, data)
}

/** 公司水平衡汇总报表详情 */
export function loadReportDataOfSummaryCompanyWeight(time: string) {
  return get<{
    enterpriseName: string
    year: number
    month: number
    enterpriseList: any[]
  }>(`${controller}/totalDetail/water`, { time })
}

/** 公司分类用电情况汇总报表详情 */
export function loadReportDataOfSummaryCompanyElectricityCategory(time: string) {
  return get<{
    [key: string]: any
    enterpriseName: string
    year: number
    month: number
    dataList: any[]
    lastMonthTotalData: any
  }>(`${controller}/totalDetail/electricitySort`, { time })
}

/** 公司主要耗能设备统计表详情查看 */
export function loadReportDataOfSummaryCompanyPrimaryElectricityDevice(
  time: string,
  formId: REPORT_FORM_ID,
) {
  return get<{
    enterpriseName: string
    year: number
    month: number
    equipmentList: any[]
  }>(`${controller}/totalDetail/equipment`, { time, formId })
}

/** 汇总报表导出 */
export async function exportFileOfReportSummary(time: string, reportId: REPORT_ID) {
  return await request({
    url: `${controller}/total/export`,
    method: 'GET',
    responseType: 'blob',
    params: { time, reportId },
  })
}

/** 报表审核-查询企业下拉选项 */
export function loadEnterpriseListOfReport() {
  return get(`${controller}/getSubsidiaryInfo`)
}

/** 报表审核-一键审核 */
export function auditReportAll(time: string) {
  return get(`${controller}/instantAudit`, { time })
}

/** 报表审核-一键导出列表 */
export async function auditExportReportStatusAll(params: {
  time: string
  status?: REPORT_AUDIT_STATUS
  socialCreditCode?: string
  reportName?: string
}) {
  return await request({
    url: `${controller}/exportAuditPage`,
    method: 'GET',
    responseType: 'blob',
    params,
  })
}

/** 报表填报-电量电费报表-查询外转供单位 */
export function loadOutsideCompanyList(year: number, socialCreditCode: string) {
  return get<string[]>(`${controller}/electricity/getZgdName`, {
    year,
    socialCreditCode,
  })
}

export function getReportName() {
  return get<string[]>(`${controller}/report/getReportName`)
}
